const axios = require("axios");

class ImgService {
  constructor() {}
  static async getimg(url) {
    const response = await axios.get(url, { responseType: "arraybuffer" });
    const buffer = Buffer.from(response.data, "binary");
    const base64Image = buffer.toString("base64");
    return base64Image;
  }

  static async getImg64(url) {
    try {
      url = url.split(",")[0];
      // 替换成内网地址，解决cdn地址无法访问的问题
      url =
        url.replace(
          "https://cdn.cellsplus.top",
          "https://cells-plus.oss-cn-guangzhou-internal.aliyuncs.com"
        ) + "?x-oss-process=image/resize,h_150";
      // url =
      //     url.replace(
      //       "https://cdn.cellsplus.top",
      //       "https://cells-plus.oss-cn-guangzhou.aliyuncs.com"
      //     ) + "?x-oss-process=image/resize,h_150";

      const base64Image = await ImgService.getimg(url);
      return `data:image/jpeg;base64,${base64Image}`;
    } catch (error) {
      throw error;
    }
  }
}
module.exports = {
  ImgService,
};
